package com.xhsj.user.lsm.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhsj.user.sms.base.BaseService;
import com.xhsj.user.lsm.entity.TbBadgesProcessOpinion;
import com.xhsj.user.lsm.mapper.TbBadgesProcessOpinionMapper;
import com.xhsj.user.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.*;

/**
 * <p>
 * 审核意见表 服务实现类
 * </p>
 *
 * @author suxiaolin
 * @since 2020-03-16
 */
@Service
@Transactional(rollbackFor = Exception.class)
//@CacheConfig(cacheNames = "TbBadgesProcessOpinion")
public class TbBadgesProcessOpinionService extends BaseService<TbBadgesProcessOpinionMapper, TbBadgesProcessOpinion>{
    @Autowired
    private TbBadgesProcessOpinionMapper dao;

    @Override
    //@CacheEvict(cacheNames="TbBadgesProcessOpinion", allEntries=true)
    public boolean save(TbBadgesProcessOpinion entity) {
        return super.save(entity);
    }

    @Override
    //@Cacheable(key = "#id",sync = true)
    public TbBadgesProcessOpinion getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    //@CacheEvict(cacheNames="TbBadgesProcessOpinion", allEntries=true)
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    //@CacheEvict(cacheNames="SysDict", allEntries=true)
    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }

    //@Cacheable(key = "#data",sync = true)
    public IPage<TbBadgesProcessOpinion> selectPage(Map<String,Object> data) {
        int currentPage=1;
        if (!Objects.isNull(data.get("page"))) {
            currentPage=(Integer) data.get("page");
        }
        int pageSize=20;
        if (!Objects.isNull(data.get("size"))) {
            pageSize=(Integer) data.get("size");
        }

        TbBadgesProcessOpinion entity = JSON.parseObject(JSON.toJSONString(data), TbBadgesProcessOpinion.class);
        Page<TbBadgesProcessOpinion> page = new Page(currentPage,pageSize);
        QueryWrapper<TbBadgesProcessOpinion> wrapper = new QueryWrapper(entity);
        return super.selectPage(page, wrapper);
    }

    @Override
    //@Cacheable(value = "TbBadgesProcessOpinion",sync = true)
    public List<TbBadgesProcessOpinion> selectAll() {
        return super.selectAll();
    }

    //    获取审批意见
    public   List<Map<String, Object>> findAuditOpinionList(String dataId){
        //获取节点id


        List<String> nodeList= new ArrayList<>();
        List<String> nodeListOpinion= new ArrayList<>();
        nodeList.add("1d44e4303a95439284cb92eefca65156");
        nodeList.add("5e8d804d54b2485391d10231fdddf4cc");
        nodeList.add("71a072f0acf646a4bea56ebb9958246b");
        nodeList.add("9845d31972b94a27917d00f244240d99");
        List<Map<String, Object>> auditOpinionList = dao.findAuditOpinionList(dataId);
        for (Map<String, Object> auditOpinion: auditOpinionList
             ) {
            nodeListOpinion.add(String.valueOf(auditOpinion.get("nodeId")));
            if(StringUtils.isBlank(auditOpinion.get("opinionText")) ){
                auditOpinion.put("opinionText",null);
            }
            if(StringUtils.isBlank(auditOpinion.get("status")) ){
                auditOpinion.put("status","");
            }
            if(StringUtils.isBlank(auditOpinion.get("date")) ){
                auditOpinion.put("date",null);
            }
        }
        nodeList.removeAll(nodeListOpinion);
        for (String nodeId : nodeList
             ) {
                Map opioionMap=new HashMap();
                opioionMap.put("nodeId",nodeId);
                opioionMap.put("opinionText",null);
                opioionMap.put("status","");
                opioionMap.put("date",null);
                auditOpinionList.add(opioionMap);
        }

        return  auditOpinionList;
    }

}
